package com.gosuncn.FileCopy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 初始化派出所编号文件
 *
 * @Author: zhangxiaolong
 * @Date: 2021/9/6 14:56
 */
@Service
public class InitPoliceCodeServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(InitPoliceCodeServiceImpl.class);


    private Map<String, String> map = new HashMap<>();
    private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();


    /**
     * 初始化派出所编码
     *
     * @author zxl
     * @date 2021/9/6
     */
    @PostConstruct
    public void init() {
        //1、加载里面的文件--》Map
        Resource resource = resolver.getResource("classpath:PoliceStationCode");
        alarmCodeMap(resource);
        //2、加载外面的，覆盖map
        resource = resolver.getResource("file:PoliceStationCode");
        //当资源文件存在 ture  不存在false
        if (resource.exists()) {
            //重新执行初始化，覆盖map
            alarmCodeMap(resource);
        }
    }


    /***
     *读取文件中报警编码生成map
     * @author zxl
     * @date 2021/9/7
     */
    public void alarmCodeMap(Resource resource) {
        InputStream is = null;
        BufferedInputStream bis = null;
        //暂存stringBuffer
        StringBuffer stringBuffer = new StringBuffer();
        try {
            is = resource.getInputStream();
            bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                //将数据转为string类型
                String s = new String(buffer, 0, len);
                //写入stringBuffer
                stringBuffer.append(s);
            }
            //转string类型进行分割
            String s = stringBuffer.toString();
            String[] strings = s.split("\n");
            //遍历每一条数据
            for (String str : strings) {
                String[] str1 = str.split(",");
                //存入map
                map.put(str1[0], str1[1]);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 更改包厢派出所编码
     *
     * @author zxl
     * @date 2021/9/6
     */
//    public void modifyPoliceCode(PtsBoxConfig ptsBoxConfig) {
//        //如果查询的派出所在map，key集合里面
//        if (map.containsKey(ptsBoxConfig.getOrgName())) {
//            //通过表中的派出所名获取map存的新编码
//            ptsBoxConfig.setOrgCode(map.get(ptsBoxConfig.getOrgName()));
//        }
//    }
//
//    /**
//     * 更改报警派出所编码
//     *
//     * @author zxl
//     * @date 2021/9/6
//     */
//    public void modifyAlarmStationCode(Callpolice callpolice) {
//        //如果查询的派出所在map，key集合里面
//        if (map.containsKey(callpolice.getBoxOrgName())) {
//            //通过表中的派出所名获取map存的新编码
//            callpolice.setBoxOrgCode(map.get(callpolice.getBoxOrgName()));
//        }
//    }
}
